﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.IO;

namespace Historia.Formulaires
{
    /// <summary>
    /// Interaction logic for frmProjet.xaml
    /// </summary>
    public partial class frmProjet : Window
    {
        #region Membres

        protected DataManager _dm = null;

        #endregion

        #region Constructeurs


        public frmProjet()
        {
            InitializeComponent();
        }

        #endregion

        #region Propriétés

        #endregion

        #region Méthodes

        public bool? ShowFormDialog(ref DataManager dataManager) {

            this._dm = dataManager;
            this.LoadData();
            this.DisplayData();
            return this.ShowDialog();
        }

        protected void LoadData() { 
        
        }

        protected void DisplayData() {

            DataRow dr = this._dm.DB.Tables[DataManager.PROJET].Rows[0];
            this.txtTitre.Text = dr.Field<string>("titre");
            this.txtAuteur.Text = dr.Field<string>("auteur");
            this.txtAlias.Text = dr.Field<string>("alias");
            this.cboDicipline.Text = dr.Field<string>("dicipline");
            this.cboStyle.Text = dr.Field<string>("style");
            this.dtpDateDebut.SelectedDate = dr.Field<DateTime>("date_debut");
            this.dtpDateFin.SelectedDate = dr.Field<DateTime?>("date_fin");
            this.txtSynopsys.Text = dr.Field<string>("synopsys");
            this.AfficherImage();
        
        }

        protected void ReadData() { 
            DataRow dr = this._dm.DB.Tables[DataManager.PROJET].Rows[0];
            dr.SetField<string>("titre", this.txtTitre.Text);
            dr.SetField<string>("auteur", this.txtAuteur.Text);
            dr.SetField<string>("alias", this.txtAlias.Text);
            dr.SetField<string>("dicipline", this.cboDicipline.Text);
            dr.SetField<string>("style", this.cboStyle.Text);
            dr.SetField<DateTime?>("date_fin", this.dtpDateFin.SelectedDate);
            dr.SetField<string>("synopsys", this.txtSynopsys.Text);
            
            this._dm.DB.Tables[DataManager.PROJET].AcceptChanges();
        }

        protected void ChangerImage()
        {
            DataRow dr = this._dm.DB.Tables[DataManager.PROJET].Rows[0];
            // Configure la fenêtre d'ouverture
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".jpg"; // Default file extension
            dlg.Filter = "Toute les images |*.bmp;*.gif;*.jpg;*.png|BMP |*.bmp|GIF |*.gif|JPEG |*.jpg|PNG |*.png"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
               string newFileName = "projet_" + this.txtTitre.Text + dlg.FileName.Substring(dlg.FileName.LastIndexOf("."));
                if (this._dm.CopierFichier(dlg.FileName, dr.Field<string>("photo"),newFileName))
                {
                    dr.SetField<string>("photo", newFileName);
                    this.AfficherImage();
                }
            }
        }

        protected void RemoveImage()
        {
            DataRow dr = this._dm.DB.Tables[DataManager.PROJET].Rows[0];
            this._dm.RemoveFile(dr.Field<string>("photo"));
            dr.SetField<string>("photo", null);
            this.AfficherImage();
           
        }

        protected void AfficherImage()
        {
            DataRow dr = this._dm.DB.Tables[DataManager.PROJET].Rows[0];
            //On affiche l'image
            if (!string.IsNullOrEmpty(dr.Field<string>("photo")))
            {
                this.imgProjet.Source = this._dm.GetImage(dr.Field<string>("photo"));
            }
            else
            {
                this.imgProjet.Source = new BitmapImage(new Uri("pack://application:,,,/Historia;component/Images/ImageNone.png"));
            }

        }

        #endregion

        private void btnAnnuler_Click(object sender, RoutedEventArgs e)
        {
            this._dm.DB.Tables[DataManager.PROJET].RejectChanges();
            this.DialogResult = false;
            this.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.ReadData();
            this.DialogResult = true;
            this.Close();
        }

        private void btnChangerImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ChangerImage();
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        private void btnRemoveImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.RemoveImage();
            }
            catch (Exception ex)
            {

                throw;
            }
        }

    }
}
